﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Facebook.Schema;
using Microsoft.DeepZoomTools;
using PivotCollectionTools.BasicCollection;

namespace test_app
{
    public class FacebookUser
    {
        public FacebookUser(user u)
        {
            this.Image = u.pic_big;

            //this.DzImage = new Image(Image);
            this.Activities = u.activities.AsHtmlSafeListOfValues();
            this.Books = u.books.AsHtmlSafeListOfValues();
            this.Interests = u.interests.AsHtmlSafeListOfValues();
            this.Movies = u.movies.AsHtmlSafeListOfValues();
            this.Music = u.music.AsHtmlSafeListOfValues();
            this.Tv = u.tv.AsHtmlSafeListOfValues();
            this.Desc = u.about_me.AsHtmlFriendlyString().AsValueOrBlank();
            this.Name = u.name;
            this.FirstName = u.first_name.AsValueOrUnknown();
            this.LastName = u.last_name.AsValueOrUnknown();
            this.BirthDate = u.birthday.AsValueOrUnknown().Split(',')[0];
            this.PoliticalViews = u.political.AsValueOrUnknown();
            this.Sex = u.sex.AsValueOrUnknown();
        }
        #region Data Elements
        //public Image DzImage { get; set; }
        private List<string> _books = new List<string>();
        private List<string> _activities = new List<string>();
        private List<string> _interests = new List<string>();
        private List<string> _movies = new List<string>();
        private List<string> _music = new List<string>();
        private List<string> _tv = new List<string>();


        [PivotItem(FacetDisplayName = "First Name", FacetType = FacetTypes.String, IsFacet = true)]
        public string FirstName { get; set; }

        [PivotItem(FacetDisplayName = "Last Name", FacetType = FacetTypes.String, IsFacet = true)]
        public string LastName { get; set; }


        private string _name;

        [PivotItem(IsName = true)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _desc;

        [PivotItem(IsDescription = true)]
        public string Desc
        {
            get { return _desc; }
            set { _desc = value; }
        }

        [PivotItem(FacetDisplayName = "Books", FacetType = FacetTypes.String, IsFacet = true, IsCollection = true)]
        public List<string> Books
        {
            get { return _books; }
            set { _books = value; }
        }

        private string _image;

        [PivotItem(IsImage = true)]
        public string Image
        {
            get { return _image; }
            set { _image = value; }
        }

        private string _sex;

        [PivotItem(FacetDisplayName = "Gender", FacetType = FacetTypes.String, IsFacet = true)]
        public string Sex
        {
            get { return _sex; }
            set { _sex = value; }
        }

        private string _relationshipStatus;

        [PivotItem(FacetDisplayName = "Relationship Status", FacetType = FacetTypes.String, IsFacet = true)]
        public string RelationshipStatus
        {
            get { return _relationshipStatus; }
            set { _relationshipStatus = value; }
        }

        private string _politicalViews;

        [PivotItem(FacetDisplayName = "Political Views", FacetType = FacetTypes.String, IsFacet = true)]
        public string PoliticalViews
        {
            get { return _politicalViews; }
            set { _politicalViews = value; }
        }

        private string _birthDate;

        [PivotItem(FacetDisplayName = "Birth Date", FacetType = FacetTypes.String, IsFacet = true)]
        public string BirthDate
        {
            get { return _birthDate; }
            set { _birthDate = value; }
        }

        [PivotItem(FacetDisplayName = "Activities", FacetType = FacetTypes.String, IsFacet = true, IsCollection = true)]
        public List<string> Activities
        {
            get { return _activities; }
            set { _activities = value; }
        }

        [PivotItem(FacetDisplayName = "TV Shows", FacetType = FacetTypes.String, IsFacet = true, IsCollection = true)]
        public List<string> Tv
        {
            get { return _tv; }
            set { _tv = value; }
        }

        [PivotItem(FacetDisplayName = "Music", FacetType = FacetTypes.String, IsFacet = true, IsCollection = true)]
        public List<string> Music
        {
            get { return _music; }
            set { _music = value; }
        }

        [PivotItem(FacetDisplayName = "Movies", FacetType = FacetTypes.String, IsFacet = true, IsCollection = true)]
        public List<string> Movies
        {
            get { return _movies; }
            set { _movies = value; }
        }

        [PivotItem(FacetDisplayName = "Intrests", FacetType = FacetTypes.String, IsFacet = true, IsCollection = true)]
        public List<string> Interests
        {
            get { return _interests; }
            set { _interests = value; }
        }
        #endregion
    }
    public static class FaceBookDataHelper
    {
        /// <summary>
        /// this method does a pretty decent job of converting strings that facebook returns for something that is more consistant
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<string> AsListOfValues(this string str)
        {
            //List<string> items = new List<string>();
            if (str == null)
                return new List<string>();
            var data = str.Split(new[] { ",", Environment.NewLine, "\r", "\n", "\r\n", "\n\r" }, StringSplitOptions.RemoveEmptyEntries).
                            Select(
                            s => HttpUtility.HtmlEncode(s).Replace(Environment.NewLine, "").Trim().ToLower()).Where(s => s.Length > 1).ToList();
            return data;

        }
        /// <summary>
        /// this method does the same as AsListOfValues but also HtmlEncodes it first
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<string> AsHtmlSafeListOfValues(this string str)
        {
            return str.AsListOfValues();
        }
        /// <summary>
        /// tries to strip out unfriendly characters
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string AsHtmlFriendlyString(this string str)
        {
            return HttpUtility.HtmlEncode(str);
        }
        public static string AsValueOrUnknown(this string str)
        {
            return (str == null) ? "Unknown" : str;
        }
        public static string AsValueOrBlank(this string str)
        {
            return (str == null) ? "Blank" : str;
        }
    }
}
